// components/deviceLabel/deviceLabel.js

// import Area
const cloud = require('../../utils/cloudFunction')
const con = require('../../utils/control')

Component({
    /**
     * Component properties
     */
    properties: {
        roomId: {
            type: String,
            value: ""
        },
        roomMac: {
            type: Number,
            value: 0
        },
        /**
         * 设备的类型
         * => "OLD" = 传统灯（只能开关）
         * => "PURE" = 单色灯（能改开关亮度）
         * => "CCT" = 双色温灯
         * => "RGB" = 彩色灯（包含修改色温）
         */
        devType: {
            type: String,
            value: "PURE"
        },
        devName: { // 单个设备的名称
            type: String,
            value: "test"
        },
        devId: {
            type: String,//不能是Number？
            value: "id"
        },
        devMac: {
            type: Number,
            value: 0
        },
        devStatus: { // 单个设备的工作状态，“null” = 打开，“false” = 关闭
            type: Number,
            value: 0
        },
    },

    /**
     * Component initial data
     */
    data: {
        clock: '',
        devIconSrc: "/image/icon/brightness_dim.png",
        label: {
            // 禁用下的样式颜色 style
            background: "#363636", // 标签背景色
            switch: "#000000", // 开关背景色
        }
        // devName: "test",
        // devType: [{
        //     name: "pure",
        //     src = "/image/icon/brightness_dim.png"
        // }, {
        //     name: "cct",
        //     src = "/image/icon/device_cct.png"
        // }, {
        //     name: "rgb",
        //     src = "/image/icon/device_rgb.png"
        // }]

    },
    /**
     * 组件的内部生命周期，详见 开发文档 - 组件生命周期
     * https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html
     */
    lifetimes: {
        // 渲染组件时，按外部传值更新组件样式
        attached() {
            // this.sender = new Sender()
            // console.log(this.properties.devId);
            this.updataDevIcon()
            this.data.clock = setInterval(() => {
                this.refreshLayout()
            }, 500);
        },

        detached() {
            // 在组件实例被从页面节点树移除时执行
            clearInterval(this.data.clock);
        },

    },

    /**
     * 组件的所在页面的生命周期，详见 开发文档 - 组件生命周期
     * https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/lifetimes.html
     */
    pageLifetimes: {
        show() {
            // // console.log(this.properties.roomId);
            // this.setData({
            //     devStatus: wx.getStorageSync((this.properties.devId).toString()),
            //     // devStatus:false
            // })
            // // console.log(this.data.bnForCct);
            // console.log('ready=' + this.properties.devStatus);
        },

    },

    /**
     * Component methods
     */
    methods: {
        // 单个设备控制开关
        devControl(e) {
            if (e.detail.value == false) {
                cloud.devControl(this.properties.devId, null, 0)
                con.turnOff(this.properties.devMac)
                this.setData({
                    devStatus: false,
                })
            } else {
                cloud.devControl(this.properties.devId, null, 50)
                con.turnOn(this.properties.devMac)
                this.setData({
                    devStatus: true,
                })
            }
        },
        // refresh layout
        refreshLayout() {
            if (this.properties.devStatus) {
                this.setData({
                    label: {
                        // active style
                        background: "(left, #0678AC, #33C9EA)",
                        switch: "#2E8298",
                    }
                })
            } else {
                this.setData({
                    label: {
                        // disable style
                        background: "#363636",
                        switch: "#494B4A",
                    },
                })
            }
        },

        // 防止冒泡
        nothing() {

        },


        // updata icon
        updataDevIcon() {
            switch (this.properties.devType) {
                case "OLD":
                    this.setData({
                        devIconSrc: "/image/icon/switch.png"
                    })
                    break;

                case "PURE":
                    this.setData({
                        devIconSrc: "/image/icon/brightness_dim.png"
                    })
                    break;

                case "CCT":
                    this.setData({
                        devIconSrc: "/image/icon/device_cct.png"
                    })
                    break;

                case "RGB":
                    this.setData({
                        devIconSrc: "/image/icon/device_rgb.png"
                    })
                    break;

                default:
                    break;
            }
        },

        // go to panel
        openPanel(e) {
            console.log(this.properties.devId + " dev into Panel")
            
            let control = 'isDev=1&&'
            let status = 'lampStatus=' + this.properties.devStatus + '&&'
            let id = 'devId=' + this.properties.devId + '&&'
            let name = 'title=' + this.properties.devName + '&&'
            let decMac = 'devMac=' + this.properties.devMac + '&&'
            let roomId = 'roomId=' + this.properties.roomId + '&&'
            let roomMac = 'roomMac=' + this.properties.roomMac + '&&'

            switch (this.properties.devType) {
                case "old":
                    wx.navigateTo({
                        url: '/pages/index/panel/oldPanel/oldPanel',
                    })
                    break;

                case "pure":
                    wx.navigateTo({
                        url: '/pages/index/panel/purePanel/purePanel',
                    })
                    break;

                case "CCT":
                    wx.navigateTo({
                        url: '/pages/index/panel/cctPanel/cctPanel?' + control + status + id + name + decMac + roomId + roomMac,
                    })
                    break;

                case "rgb":
                    wx.navigateTo({
                        url: '/pages/index/panel/rgbPanel/rgbPanel',
                    })
                    break;

                default:
                    break;
            }
        },
    },

})